#ifndef _PROJ_TYPE_H_
#define _PROJ_TYPE_H_
#include "stdint.h"
#include "stm32f10x.h"
#include "key.h"

#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))

/*
 * Linux Kernel ¶Ô¶¨Ê±Æ÷µÄÓ¦ÓÃºê
 */
// #define typecheck(type,x) \
// ({        type __dummy; \
//         typeof(x) __dummy2; \
//         (void)(&__dummy == &__dummy2); \
//         1; \
// })

#define typecheck(type,x) (1)

#define time_after(a,b) \
	(typecheck(unsigned long, a) && \
	 typecheck(unsigned long, b) && \
	 ((long)(b) - (long)(a) < 0))
#define time_before(a,b) time_after(b,a)

#define time_after_eq(a,b) \
	(typecheck(unsigned long, a) && \
	 typecheck(unsigned long, b) && \
	 ((long)(a) - (long)(b) >= 0))
#define time_before_eq(a,b) time_after_eq(b,a)

#ifdef CONFIG_HZ
	#define HZ  (CONFIG_HZ)
#else
	#define HZ  (1000)	
#endif

#ifdef CONFIG_MS
	#define _1MS (CONFIG_MS)
#else
	#define _1MS (1)
#endif



//ÖØ¶¨Òå±ðÃû

#define Key_Port_Set GPIOA
#define Key_Pin_Set KEY_A

#define Key_Port_Enter GPIOA
#define Key_Pin_Enter KEY_A

#define Key_Port_Up GPIOA
#define Key_Pin_Up KEY_A

#define Key_Port_Down GPIOA
#define Key_Pin_Down KEY_A

#define Key_Port_Left GPIOA
#define Key_Pin_Left KEY_A

#define Key_Port_Right GPIOA
#define Key_Pin_Right KEY_A

//±£´æµ±Ç°²¨³¤¡¢¸÷¸öÄ£Ê½ÏÂµÄadc¡¢dacÖµ£¬ÌáÉýÓÃ»§ÌåÑé
struct fast_switch {
	int32_t enable;
	int32_t set;

	//CH1
	uint16_t ch1_adc_off;///<¿ìËÙÐ£×¼ch1
	uint16_t ch1_adc_cw;///<
	uint16_t ch1_adc_270;///<
	uint16_t ch1_adc_1k;///<
	uint16_t ch1_adc_2k;///<

	uint16_t ch1_dac_off;
	uint16_t ch1_dac_cw;
	uint16_t ch1_dac_270;
	uint16_t ch1_dac_1k;
	uint16_t ch1_dac_2k;

	//CH2
	uint16_t ch2_adc_off;
	uint16_t ch2_adc_cw;
	uint16_t ch2_adc_270;
	uint16_t ch2_adc_1k;
	uint16_t ch2_adc_2k;

	uint16_t ch2_dac_off;
	uint16_t ch2_dac_cw;
	uint16_t ch2_dac_270;
	uint16_t ch2_dac_1k;
	uint16_t ch2_dac_2k;

	//CH3
	uint16_t ch3_adc_off;
	uint16_t ch3_adc_cw;
	uint16_t ch3_adc_270;
	uint16_t ch3_adc_1k;
	uint16_t ch3_adc_2k;

	uint16_t ch3_dac_off;
	uint16_t ch3_dac_cw;
	uint16_t ch3_dac_270;
	uint16_t ch3_dac_1k;
	uint16_t ch3_dac_2k;
};
struct ctrl_param { //ÐèÒª¿ØÖÆµÄ²ÎÊý
	int32_t lastset;
	int32_t set; ///<ÉèÖÃÖµ²ÎÊýÀ©´ó1000±¶
	int32_t cur; ///<Êµ¼ÊÖµ
	uint16_t adc;///<dac
	uint16_t dac;
	int8_t  ld;
	uint16_t vol;
	uint8_t level;//ÄÜ¿ØÖÆµÄ¼¶±ð
	int32_t temp;
};


struct device_ls {
	// 具有刻度意义的描述
	uint32_t ch;		// 工作通道
	uint32_t mode;		// 工作模式
	float    o_dbm;		// 预输出功率dbm

	// 设备控制描述
	uint16_t adc;		// 输入反馈ADC
	uint16_t dac;		// 输出控制DAC
};

struct device_opm {
	// 具有刻度意义的描述
	float    i_dbm;		// 由adc、lv转换后的对数
	float    k_ref_1310;  	// 相对1310

	// 设备控制描述
	uint16_t adc;		// 输入ADC
	int      lv;		// 当前档位
	// TODO 
	// struct adj_xxx *padj;// 校准参数列表	
};

struct adj_power_flash {
	uint32_t flag;///<±êÖ¾£¬µ±ÆäÎª0xAABBCCDDÊ±ºò±íÊ¾ÏÂÃæµÄÊý¾ÝÓÐÐ§£¬·ñÔòÏÂÃæÊý¾ÝÇåÁã

	float _adc;///<Ð£×¼ADCÖµ£¬¼ÇÂ¼ÔÚÊä³ö10dBmÊ±µÄADCÖµ£¬@see Ctrl_Power

	//float _1310_270;//DAC
	float _dac;///<Ð£×¼DACÖµ£¬¼ÇÂ¼ÔÚÊä³ö10dBmÊ±µÄDACÖµ£¬Ìá¸ß×Ô¶¯µ÷½ÚËÙ¶È£¬@see AutoCtrlPower

	//////Ê¹ÓÃÔ­±£ÁôÄÚÈÝ·Ö±ðÊÇ3¸ö¼¤¹âÆ÷Í¨µÀµÄADC¡¢DACÐ£×¼Öµ
	// ÆäÖÐ0-3·Ö±ð¶ÔÓ¦Ä¬ÈÏµÄ1310¡¢1490¡¢1550¼¤¹âÆ÷
	float _adc1;//float _1310_1k;
	float _dac1;//float _1310_2k;
	float _adc2;//float _1550cw;
	float _dac2;//float _1550_270;
	float _adc3;//float _1550_1k;
	float _dac3;//float _1550_2k;

	float _5_adc1;//float _1310_1k;
	float _5_dac1;//float _1310_2k;
	float _5_adc2;//float _1550cw;
	float _5_dac2;//float _1550_270;
	float _5_adc3;//float _1550_1k;
	float _5_dac3;//float _1550_2k;


	//////////
	uint8_t sn[28];
	uint8_t _650_en;///<650Ê¹ÄÜ
	uint8_t _1310_en;///<ch1£¨1310£©Ê¹ÄÜ
	uint8_t _1490_en;///<ch2£¨1490£©Ê¹ÄÜ
	uint8_t _1550_en;///<ch3£¨1550£©Ê¹ÄÜ

	uint8_t en_ch1;///<ch1£¨1310£©Ê¹ÄÜ
	uint8_t en_ch2;///<ch2£¨1490£©Ê¹ÄÜ
	uint8_t en_ch3;///<ch3£¨1550£©Ê¹ÄÜ


	//ÌùÅÆ
	uint32_t _logo_addr;///<logoµØÖ·£¬±£Áô
	uint16_t _logo_backcolor;///<logo±³¾°É«£¬±£Áô
	uint16_t _logo_w;///<logo¿í¶È£¬¿í¸ßÓÐ×î´óÏÞÖÆ£¬±£Áô
	uint16_t _logo_h;///<logo¸ß¶È£¬±£Áô

	uint16_t _ch1wave;///<ch1²¨³¤£¬Ä¬ÈÏ1310
	uint16_t _ch2wave;///<ch2²¨³¤£¬Ä¬ÈÏ1490
	uint16_t _ch3wave;///<ch3²¨³¤£¬Ä¬ÈÏ1550

	uint16_t lv1_low;
	uint16_t lv1_high;
	uint16_t lv2_low;
	uint16_t lv2_high;
	uint16_t lv3_low;
	uint16_t lv3_high;
	uint16_t lv4_low;
	uint16_t lv4_high;
	uint16_t lv5_low;
	uint16_t lv5_high;
	uint16_t lv6_low;
	uint16_t lv6_high;
	uint16_t lv7_low;
	uint16_t lv7_high;

	int cur_lv;

};
struct point {
	int16_t x;
	int16_t y;
};


struct system_param {
	char sn[32];
	short ch1_10_adc;
	short ch1_10_dac;
	short ch2_10_adc;
	short ch2_10_dac;
	short ch3_10_adc;
	short ch3_10_dac;

	int ch1_wave;
	int ch2_wave;
	int ch3_wave;

	int ch1_en;
	int ch2_en;
	int ch3_en;

	short lv1_low;
	short lv1_high;
	short lv2_low;
	short lv2_high;
	short lv3_low;
	short lv3_high;
	short lv4_low;
	short lv4_high;
	short lv5_low;
	short lv5_high;
	short lv6_low;
	short lv6_high;
	short lv7_low;
	short lv7_high;

	float _1550_ref_1310;
	float _1490_ref_1310;
	float _1625_ref_1310;
	float _850_ref_1310;
};

extern struct system_param sys_param;
// typedef struct _RECT
// {
// 	int32_t left;
// 	int32_t top;
// 	int32_t right;
// 	int32_t bottom;
// }RECT;

// typedef struct win_class
// {
// 	uint32_t id;
// 	RECT rc;
// }win_class;

// extern struct ctrl_param g_power;
extern struct adj_power_flash g_adj_power;//Ð£×¼²ÎÊý
// extern volatile u16 powerDownDelayCnt;//¹Ø»ú³¤°´ÑÓÊ±
// extern uint32_t hackval ;//½øÈëºóÌ¨ÊäÈëµÄÃÜÂë
// extern uint32_t hackflag ;//ÊÇ·ñÊäÈëÃÜÂë±êÖ¾
// extern volatile u8 g_red_onoff ;//
// extern volatile int8_t g_red_mode ;//ºìµÆÏÔÊ¾Ä£Ê½£¬³£ÁÁ¡¢ÉÁË¸¡¢¹Ø±Õ
// extern volatile u8 g_red_delay_100ms ;//ºìµÆÉÁË¸0.5sÑÓÊ±
// extern volatile u8 g_onoff_en ;//Ê¹ÄÜ¹Ø»ú£¬·ÀÖ¹¿ª»úºóÔÙ¹Ø»ú
// extern volatile u8 g_autoctrlpower_en;

// extern volatile u8 g_key_timer_100ms ;//°´¼ü¶¨Ê±Æ÷£¬ÓÃÓÚÅÐ¶Ï³¤°´ÏÂºÍµ¥»÷£¬¸´ÓÃºìµÆºÍ¶¨Ê±¹Ø»ú°´¼ü
// extern volatile uint16_t g_batter_delay ;//µç³ØË¢ÐÂÏÔÊ¾ÑÓÊ±
// /////////////////////
// extern volatile uint16_t g_power_down;
// extern volatile uint16_t g_ad_ms , g_adjust_ms , g_lcdbug_ms , g_usart_ms , g_lcdlisten_ms , g_debug_ms , g_redbug_ms; //ad²ÉÑù¼ä¸ô
// extern volatile uint16_t g_adc[200];//ad²ÉÑùÊý
// extern volatile uint16_t ADCConvertedValue[2000];//AD²ÉÑùDMA»º´æ

// extern int8_t g_recvflag ;//´®¿Ú½ÓÊÕ±êÖ¾
// extern volatile uint8_t strout[256];

/*****************************************************************************
Ô­À´µÄ¶¨Òå
*****************************************************************************/
// extern volatile u16 SysTickCounter;
// extern volatile u16 Timer_State ;	    //¶¨Ê±Æ÷×´Ì¬Ö¸Ê¾,  OFF,  5min  £¬10min  £¬15min  £¬30min  £¬60min ×´Ì¬
// extern volatile u16 Timer_Counter ;
// extern volatile u16 Wavelength_Selection_state ;	 //0 = ¹Ø±Õ×´Ì¬ 1 = 1310nm 2 = 1495nm 3 = 1550nm 	4 = ºì¹â
// extern volatile u16 Operating_Mode ;	//0 = CW¡¢ 1 = PW270Hz¡¢2 = 1KHz¡¢3 = 2KHz  Á¬Ðø¹â/Âö³å¹âÑ¡Ôñ
// extern volatile u8  Batter_Lightning;
// extern volatile u8  LCD_GetPoint_EN ;   //·ÀÖ¹´¥ÃþÆÁÖØ¸´²Ù×÷£¬Ïàµ±ÓÚ°´¼üÈ¥¶¶¶¯,¿ÉÒÔ½øÐÐ´¥ÃþÆÁ²É¼¯¡£
// extern volatile u8  LCD_GetPoint_Counter ; //´¥ÃþÆÁÊ¹ÄÜ¼ÆÊýÖµ¡£¡£
extern volatile unsigned long  jiffies;
// extern struct load_env sys_vals[32];
// extern volatile unsigned long isdebug;
#endif

